#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

void printHTMLHead(void)
{
    printf( "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n\"http://www.w3.org/TR/html4/loose.dtd\">\n" );
    printf( "<html>\n" );
    printf( "<head>\n" );
    printf( "<title>Read From Form</title>\n" );
    printf( "</head>\n" );
    printf( "<body>\n" );
}

void printHTMLEnd(void)
{
    printf( "</body>\n" );
    printf( "</html>\n" );
}

int findIdentifier(int position, char* str)
{
    int i = position;
	while(i < strlen(str))
    {
		if(str[i] == '=')
			break;
        ++i; 
    }
	return i;
}

int getValue(int position, char* str, char* value)
{
    int i = findIdentifier(position, str);
	++i;
    int j = 0;
    if(str[i] != '\n')
	{
		while((str[i] != '\n') && (i <= (strlen(str)-1)))
		{
			value[j] = str[i];
			++j;
			++i;
		}
	}
	value[j] = '\0';
	return (i+1);
}

char myToLower(char ch)
{
	int res = (int)ch;
	if (ch < 128)
	{
		return tolower(ch);
	}
	else if (ch < 144)
	{
		res = (int)ch + 32;
	}
	else if (ch < 160)
	{
		res = (int)ch + 96;
	}
	
	return (char)res;
}

void toLowerStr(char* str)
{
	int i;
	for (i = 0; i < strlen(str); i++)
	{
		str[i] = myToLower(str[i]);
	}
}

int StringToInt(const char * str)
{
	char * pLastChar = NULL;
	int param = strtol(str, &pLastChar, 10);
	if((*str == '\0') || (*pLastChar != '\0'))
	{
		return 0;
	}
	else
		return param;
}

void getFormData(char* data, char* lastName, char* eMail, char* reEmail, char* password, char* sex, char* day, char* month, char* year)
{
	int posPrev = getValue(0, data, lastName);
	int posNext = getValue(posPrev, data, eMail);
	posPrev = getValue(posNext, data, reEmail);
	toLowerStr(eMail);
	toLowerStr(reEmail);
	posNext = getValue(posPrev, data, password);
	posPrev = getValue(posNext, data, sex);
	posNext = getValue(posPrev, data, month);
	posPrev = getValue(posNext, data, day);
	posNext = getValue(posPrev, data, year);
}

int checkInput(char* lastName, char* eMail, char* reEmail, char* password, char* sex, char* day, char* month, char* year)
{
	if(strcmp(lastName, "") == 0)
	{
		printf("<h1>You didn't enter last name!</h1>\n");
		return 1;
	}
	if(strcmp(eMail, reEmail) != 0)
	{
		printf("<h1>Re-entering of eMail was incorrect!</h1>\n");
		return 1;
	}
	if(strcmp(reEmail, "") == 0)
	{
		printf("<h1>You didn't enter eMail!</h1>\n");
		return 1;
	}
	if(strcmp(password, "") == 0)
	{
		printf("<h1>You didn't enter password!</h1>\n");
		return 1;
	}
	if(strcmp(sex, "Select Sex:") == 0)
	{
		printf("<h1>You didn't enter sex!</h1>\n");
		return 1;
	}
	if(strcmp(month, "Month:") == 0)
	{
		printf("<h1>You didn't enter month of birthday!</h1>\n");
		return 1;
	}
	if(StringToInt(day) == 0)
	{
		printf("<h1>You didn't enter day of birthday!</h1>\n");
		return 1;
	}
	if(StringToInt(year) == 0)
	{
		printf("<h1>You didn't enter year of birthday!</h1>\n");
		return 1;
	}
	return 0;
}

void printDataToFile(FILE * File, char* lastName, char* eMail, char* reEmail, char* password, char* sex, char* day, char* month, char* year)
{
	fprintf(File, "%s\n", lastName);
    fprintf(File, "%s\n", eMail);
	fprintf(File, "%s\n", reEmail);
	fprintf(File, "%s\n", password);
    fprintf(File, "%s\n", sex);
	fprintf(File, "%s\n", month);
	fprintf(File, "%s\n", day);
    fprintf(File, "%s\n", year);
}